package com.bigshark.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigshark.api.form.BannerForm;
import com.bigshark.api.model.Banner;
import com.bigshark.api.service.BannerService;
import com.bigshark.common.utills.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 获取前6 banner
     * @return
     */
    @GetMapping("/")
    @ApiOperation(value = "获取banner",notes = "获取banner",tags = "banner接口",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "banner数量",name = "num",dataType = "Integer")})
    public R getBanner(@RequestParam(name = "num",defaultValue = "6") Integer num){
        List<Banner> bannerList =  bannerService.list(new QueryWrapper<Banner>().last("limit "+num));
        return R.ok().put("data",bannerList);
    }

    /**
     * 获取banner详情
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取banner详情",notes = "获取banner详情",tags = "banner接口",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "bannerId",name = "id",dataType = "Integer")})
    public R getBannerInfo(@PathVariable Integer id){
        Banner banner = bannerService.getById(id);
        return R.ok().put("data",banner);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除banner",notes = "删除banner",tags = "banner接口",httpMethod = "DELETE")
    @ApiImplicitParams({@ApiImplicitParam(value = "bannerId",name = "id",dataType = "Long")})
    public R deleteBanner(@PathVariable Long id){
       if( !bannerService.removeById(id)){
           return R.error("删除失败");
       }
        return R.ok();
    }

    @PostMapping("/")
    @ApiOperation(value = "添加Banner",notes = "添加Banner",tags = "banner接口",httpMethod = "POST")
    public R createBanner(@RequestBody BannerForm bannerForm){
        Banner banner = new Banner();
        try{
            banner.setAddTime(new Date());
            banner.setStatue(bannerForm.getStatue());
            banner.setImgUrl(bannerForm.getImgurl());
            banner.setUrl(bannerForm.getUrl());
            banner.setTitle(bannerForm.getTitle());
            bannerService.saveOrUpdate(banner);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改Banner",notes = "修改Banner",tags = "banner接口",httpMethod = "PUT")
    public R upDataBanner(@PathVariable Long id,@RequestBody BannerForm bannerForm){
        Banner banner = new Banner();
        banner.setId(id);
        banner.setStatue(bannerForm.getStatue());
        banner.setUrl(bannerForm.getUrl());
        banner.setImgUrl(bannerForm.getImgurl());
        banner.setTitle(banner.getTitle());
        try{
            bannerService.saveOrUpdate(banner);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }

}
